package com.ruoyi.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.system.domain.vo.SysUserMessageVo;
import com.ruoyi.system.domain.bo.SysUserMessageBo;
import com.ruoyi.system.service.ISysUserMessageService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

import java.util.List;
import java.util.Arrays;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户消息通知控制器
 * 前端访问路由地址为:/system/userMessage
 *
 * @author ruoyi
 * @date 2024-12-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/userMessage")
public class SysUserMessageController extends BaseController {

    private final ISysUserMessageService iSysUserMessageService;

    /**
     * 查询用户消息通知列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysUserMessageVo> list(SysUserMessageBo bo, PageQuery pageQuery) {
        bo.setReceiver(LoginHelper.getUserId());
        return iSysUserMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户消息通知列表
     */
    @SaCheckPermission("system:userMessage:export")
    @Log(title = "用户消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysUserMessageBo bo, HttpServletResponse response) {
        List<SysUserMessageVo> list = iSysUserMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户消息通知", SysUserMessageVo.class, response);
    }

    /**
     * 获取用户消息通知详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<SysUserMessageVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        SysUserMessageVo msg = iSysUserMessageService.queryById(id);
        if (msg == null || !LoginHelper.getUserId().equals(msg.getReceiver())){
            throw new ServiceException("消息不存在");
        }
        return R.ok(iSysUserMessageService.queryById(id));
    }

    /**
     * 新增用户消息通知
     */
    @SaCheckPermission("system:userMessage:add")
    @Log(title = "用户消息通知", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysUserMessageBo bo) {
        return toAjax(iSysUserMessageService.insertByBo(bo));
    }

    /**
     * 修改用户消息通知
     */
    @SaCheckPermission("system:userMessage:edit")
    @Log(title = "用户消息通知", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysUserMessageBo bo) {
        return toAjax(iSysUserMessageService.updateByBo(bo));
    }

    /**
     * 删除用户消息通知
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:userMessage:remove")
    @Log(title = "用户消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iSysUserMessageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 清空未读用户消息通知
     */
    @Log(title = "清空未读用户消息通知", businessType = BusinessType.UPDATE)
    @GetMapping("/read")
    public R<Void> read() {
        return toAjax(iSysUserMessageService.clearUnRead(LoginHelper.getUserId()));
    }

    /**
     * 获取未读消息数量
     *
     */
    @GetMapping("/count")
    public R<Long> unReadCount() {
        return R.ok(iSysUserMessageService.getUnReadCount(LoginHelper.getUserId()));
    }
}
